package com.Xht.www.servlet;

//后端响应用户签到的请求

import com.Xht.www.po.UserCheckinPO;
import com.Xht.www.serviceImpl.UserImpl;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/checkin")
public class CheckinServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/plain; charset=UTF-8");

        String userIdStr = request.getParameter("userId");
        String pointsEarnedStr = request.getParameter("pointsEarned");
        if (userIdStr == null || pointsEarnedStr == null) {
            response.getWriter().println("请求参数缺失，请检查");
            return;
        }
        int userId;
        int pointsEarned;
        try {
            userId = Integer.parseInt(userIdStr);
            pointsEarned = Integer.parseInt(pointsEarnedStr);
        } catch (NumberFormatException e) {
            response.getWriter().println("请求参数格式错误，请检查");
            return;
        }

        UserCheckinPO checkinPO = new UserCheckinPO();
        checkinPO.setUserId(userId);
        checkinPO.setPointsEarned(pointsEarned);

        UserImpl userService = new UserImpl();
        boolean result = userService.userCheckin(checkinPO);

        if (result) {
            response.getWriter().println("签到成功");
        } else {
            response.getWriter().println("您无法重复签到");
        }
    }
}